//
//  BSSEnterpriseCertificationBillTextFieldCell.m
//  BSSMarket
//
//  Created by 传铎 on 2020/9/27.
//  Copyright © 2020 BSS. All rights reserved.
//

#import "BSSEnterpriseCertificationBillTextFieldCell.h"
@interface BSSEnterpriseCertificationBillTextFieldCell()<UITextFieldDelegate>

@end
@implementation BSSEnterpriseCertificationBillTextFieldCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor clearColor];
        [self createUI];
        [self layoutUI];
    }
    return self;
}

- (void)createUI {
    [self.contentView addSubview:self.titleLab];
    [self.contentView addSubview:self.textField];
    [self.contentView addSubview:self.line];
}

- (void)layoutUI {

    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__kSize(22));
        make.top.mas_equalTo(__kSize(12));
    }];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.right.mas_equalTo(__kSize(- 22));
        make.left.mas_equalTo(97);
    }];
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_titleLab.mas_bottom).mas_offset(__kSize(12));
        make.left.mas_equalTo(__kSize(22));
        make.right.mas_equalTo(__kSize(- 22));
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(self.contentView);
    }];
}
- (void)setModel:(BSSEnterpriseCertificationModel *)model{
    _model = model;
    _textField.text = model.value;
    if ([model.type isEqualToString:@"5"]) {
        _textField.addSymbol = @"-";
    }else if ([model.type isEqualToString:@"6"]){
        _textField.addSymbol = @"X";
    }
        
}
#pragma mark - <#delegate#>
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return YES;
}
- (void)textFieldDidChange:(UITextField *)textField
{
   
    if (!IsEmpty(self.model.maxLength)) {
       if ([textField.text length] > [self.model.maxLength integerValue]) {
            textField.text = [textField.text substringToIndex:[self.model.maxLength integerValue]];
        }
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if ([string isEqualToString:@""]) {
        return YES;
    }
    if (!IsEmpty(self.model.maxLength)) {
       if ([textField.text length] >= [self.model.maxLength integerValue]) {
            return NO;
        }
    }
    
    
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    self.model.value  = textField.text;
   
}
#pragma mark - action

#pragma mark - setter & getter

- (UILabel *)titleLab {
    if (!_titleLab) {
        _titleLab = [UILabel new];
        _titleLab.textColor = __kColor(51, 51, 51);
        _titleLab.font = fontCount(14);
    }
    return _titleLab;
}
- (UITextField *)textField{
    if (!_textField){
        _textField = [BXTextField new];
        _textField.delegate  = self;
        _textField.addSymbol = @"-";
        _textField.font = __kFont(14);
        _textField.textColor = colorCode(@"#999999");
        _textField.textAlignment = NSTextAlignmentRight;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.autocorrectionType = UITextAutocorrectionTypeNo;
        _textField.placeholder = @"请输入";
    }
    return _textField;
}
- (UIView *)line {
    if (!_line) {
        _line = [UIView new];
        _line.backgroundColor = __kColor(238, 238, 238);
    }
    return _line;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
